package StudyCode.summary.textfile;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.*;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ModifyingPOSIXOwnershipOfAFile {
	public static void main(String[] args) throws Exception {
		Path path = Paths.get("home/docs/test.txt");
		try {
			PosixFileAttributeView view = Files.getFileAttributeView(path,
					PosixFileAttributeView.class);

			PosixFileAttributes attributes = view.readAttributes();

			System.out.println("旧分组: " + attributes.group().getName());
			System.out.println("旧所有者: " + attributes.owner().getName());
			System.out.println();

			UserPrincipalLookupService lookupService = FileSystems.getDefault()
					.getUserPrincipalLookupService();
			UserPrincipal userPrincipal = lookupService
					.lookupPrincipalByName("lee");
			GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName("lee");
			view.setGroup(groupPrincipal);
			view.setOwner(userPrincipal);
			attributes = view.readAttributes();
			System.out.println("新分组: " + attributes.group().getName());
			System.out.println("新所有者: " + attributes.owner().getName());
			System.out.println();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}
